package com.bjfl.saleManager.util;

public class HexByteDumper
{
  public static String getHexDump(byte[] src)
  {
    StringBuilder stringBuilder = new StringBuilder("");
    if ((src == null) || (src.length <= 0)) {
      return null;
    }
    for (int i = 0; i < src.length; i++)
    {
      int v = src[i] & 0xFF;
      String hv = Integer.toHexString(v);
      if (hv.length() < 2) {
        stringBuilder.append(0);
      }
      stringBuilder.append(hv);
    }
    return stringBuilder.toString().toUpperCase();
  }
  
  public static byte[] getBytesDump(String hexString)
  {
    if ((hexString == null) || (hexString.equals(""))) {
      return null;
    }
    hexString = hexString.toUpperCase();
    int length = hexString.length() / 2;
    char[] hexChars = hexString.toCharArray();
    byte[] d = new byte[length];
    for (int i = 0; i < length; i++)
    {
      int pos = i * 2;
      d[i] = ((byte)(getByteDump(hexChars[pos]) << 4 | getByteDump(hexChars[(pos + 1)])));
    }
    return d;
  }
  
  private static byte getByteDump(char c)
  {
    return (byte)"0123456789ABCDEF".indexOf(c);
  }
  
  public static String getHexIp(String ip)
  {
    String[] ips = ip.split("\\.");
    
    return getHexDump(Integer.parseInt(ips[3]))
      .concat(getHexDump(Integer.parseInt(ips[2])))
      .concat(getHexDump(Integer.parseInt(ips[1])))
      .concat(getHexDump(Integer.parseInt(ips[0])));
  }
  
  public static byte[] getBytesDump(int integer)
  {
    int byteNum = (40 - Integer.numberOfLeadingZeros(integer < 0 ? integer ^ 0xFFFFFFFF : integer)) / 8;
    
    byte[] byteArray = new byte[4];
    for (int n = 0; n < byteNum; n++) {
      byteArray[(3 - n)] = ((byte)(integer >>> n * 8));
    }
    return byteArray;
  }
  
  public static String getHexDump(int integer)
  {
    String hex = Integer.toHexString(integer & 0xFF);
    if (hex.length() == 1) {
      hex = '0' + hex;
    }
    return hex.toUpperCase();
  }
  
  public static String toStringHex(String s)
  {
    byte[] baKeyword = new byte[s.length() / 2];
    for (int i = 0; i < baKeyword.length; i++) {
      try
      {
        baKeyword[i] = ((byte)(0xFF & Integer.parseInt(s
          .substring(i * 2, i * 2 + 2), 16)));
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
    try
    {
      s = new String(baKeyword);
    }
    catch (Exception e1)
    {
      e1.printStackTrace();
    }
    return s;
  }
  
  public static String intToString(int parameter, String needFillChar, int length)
  {
    String csCode = parameter + "";
    int length1 = length - csCode.length();
    for (int i = 0; i < length1; i++) {
      csCode = needFillChar + csCode;
    }
    return csCode;
  }
  
  public static String stringToString(String parameter, String needFillChar, int length)
  {
    String csCode = parameter;
    int length1 = length - csCode.length();
    for (int i = 0; i < length1; i++) {
      csCode = needFillChar + csCode;
    }
    return csCode;
  }
}
